//
//  TTTabBarController.swift
//  NewWeiBo
//
//  Created by 吴天益 on 2017/7/22.
//  Copyright © 2017年 ttt. All rights reserved.
//

import UIKit

class TTTabBarController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        //自定义tabbar
        let tttabbar = TTTabBar()
//        self.tabBar = tabBar//只读属性,只能通过kvc赋值
        self.setValue(tttabbar, forKey: "tabBar")
        
        //设置当前服务器为代理/
        tttabbar.TTDelegate = self
        
        addChildViewController(childController: TTHomeViewController(), title: "首页", imageName: "TabBar_HomeBar")
        
        addChildViewController(childController: TTWhatViewController(), title: "消息", imageName: "TabBar_Friends")
        
        addChildViewController(childController: TTDiscoverViewController(), title: "发现", imageName: "TabBar_Businesses")
        
        addChildViewController(childController: TTMineViewController(), title: "我的", imageName: "TabBar_Assets")
        
        
    }
    
    //***重载addChildViewController方法
    func addChildViewController(childController: UIViewController,title: String,imageName: String) {
        
        //设置文字
        childController.title = title
        
        //设置图片//去渲染,使用图片自己颜色
        childController.tabBarItem.image = UIImage(named: imageName)?.withRenderingMode(.alwaysOriginal)
        childController.tabBarItem.selectedImage = UIImage(named: imageName+"_Sel")?.withRenderingMode(.alwaysOriginal)
        
        //设置tabbar文字属性
        childController.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.orange], for: .highlighted)
        childController.tabBarItem.setTitleTextAttributes([NSFontAttributeName: UIFont.systemFont(ofSize: 12)], for: .normal)
        
        //创建导航控制器
        let navi = TTNavigationController(rootViewController: childController)
        
        addChildViewController(navi)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
  
}

//MARK:
//MARK: -- 代理方法的实现,先遵守代理协议
extension TTTabBarController: TabBarDelegate{
    func turnToANewController() {
        
//        print("发消息")
        
        //出现发微博视图,可以用view来做
        let sendWeiBoView = SendWeiBoView(frame: CGRect(x: 0, y: 0, width: ScreenWidth, height: ScreenHeight))
        
        //为view的属性target赋值,使其可以使用控制器跳转界面
        sendWeiBoView.target = self
        
        //添加在window上,就可以覆盖导航栏
        let window = UIApplication.shared.keyWindow!
        window.addSubview(sendWeiBoView)

    }
}
